﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
namespace Yahoo.Yui.CompressorConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            ConfigHelper config = new ConfigHelper();
            List<YuiDirectory> listDir = config.LoadDirectorys();
            List<YuiFile> listFile = config.LoadFiles();

            ListUnExistDirectoryAndFile(listDir, listFile);

            List<string> jsFiles = GetJsFiles(listDir, listFile);
            List<string> cssFiles = GetCssFiles(listDir, listFile);
            Console.WriteLine("INFO:将要进行编译的文件");
            Console.WriteLine("INFO:js文件");
            ListFiles(jsFiles);
            Console.WriteLine("INFO:css文件");
            ListFiles(cssFiles);

            while (true)
            {
                Console.WriteLine();
                Console.WriteLine("请输入要编译的文件类型:");
                Console.WriteLine("js:只编译js文件,css:只编译css文件,all:编译js和css文件,exit:退出");
                Console.Write("请输入:");
                string input = Console.ReadLine();
                if (input == "js")
                {
                    CompressorJs(jsFiles);
                    break;
                }
                else if (input == "css")
                {
                    CompressorCss(cssFiles);
                    break;
                }
                else if (input == "all")
                {
                    CompressorJs(jsFiles);
                    CompressorCss(cssFiles);
                    break;
                }
                else if (input == "exit")
                {
                    break;
                }
            }
            Console.WriteLine();
            Console.WriteLine("Press any key to close.");

            Console.ReadKey();
        }

        /// <summary>
        /// 列出不存在的目录和文件
        /// </summary>
        /// <param name="listDir"></param>
        /// <param name="listFile"></param>
        static void ListUnExistDirectoryAndFile(List<YuiDirectory> listDir, List<YuiFile> listFile)
        {
            Console.WriteLine("INFO:进行目录检测...");
            foreach (YuiDirectory dir in listDir)
            {
                if (!dir.Exist)
                {
                    Console.WriteLine(string.Format("WARN:目录{0}不存在", dir.Path));
                }
            }
            Console.WriteLine();
            Console.WriteLine("INFO:进行文件检测...");
            string ext = null;
            foreach (YuiFile file in listFile)
            {
                if (!file.Exist)
                {
                    Console.WriteLine(string.Format("WARN:文件{0}不存在", file.Path));
                }
                else
                {
                    ext = Path.GetExtension(file.Path).ToLower();
                    if (ext == ".js" || ext == ".css")
                    {

                    }
                    else
                    {
                        Console.WriteLine(string.Format("WARN:文件类型非法,必须是.js或.css文件:{0}", file.Path));
                    }
                }
            }
        }

        static void ListFiles(List<string> files)
        {
            foreach (string file in files)
            {
                if (file.ToLower().EndsWith(".min.js") || file.ToLower().EndsWith(".min.css"))
                {
                }
                else
                {
                    Console.WriteLine(file);
                }
            }
        }

        /// <summary>
        /// 获取所有要进行编译的JS文件
        /// </summary>
        /// <param name="listDir"></param>
        /// <param name="listFile"></param>
        /// <returns></returns>
        public static List<string> GetJsFiles(List<YuiDirectory> listDir, List<YuiFile> listFile)
        {
            List<string> files = new List<string>();

            foreach (YuiDirectory dir in listDir)
            {
                if (dir.Exist)
                {
                    if (dir.Recursion)
                    {
                        files.AddRange(Directory.GetFiles(dir.Path, "*.js", SearchOption.AllDirectories));
                    }
                    else
                    {
                        files.AddRange(Directory.GetFiles(dir.Path, "*.js"));
                    }
                }
            }

            foreach (YuiFile file in listFile)
            {
                if (file.Exist && Path.GetExtension(file.Path).ToLower() == ".js")
                {
                    files.Add(file.Path);
                }
            }
            return files;
        }

        /// <summary>
        /// 获取所有要进行编译的CSS文件
        /// </summary>
        /// <param name="listDir"></param>
        /// <param name="listFile"></param>
        /// <returns></returns>
        public static List<string> GetCssFiles(List<YuiDirectory> listDir, List<YuiFile> listFile)
        {
            List<string> files = new List<string>();

            foreach (YuiDirectory dir in listDir)
            {
                if (dir.Exist)
                {
                    if (dir.Recursion)
                    {
                        files.AddRange(Directory.GetFiles(dir.Path, "*.css", SearchOption.AllDirectories));
                    }
                    else
                    {
                        files.AddRange(Directory.GetFiles(dir.Path, "*.css"));
                    }
                }
            }

            foreach (YuiFile file in listFile)
            {
                if (file.Exist && Path.GetExtension(file.Path).ToLower() == ".css")
                {
                    files.Add(file.Path);
                }
            }
            return files;
        }

        public static void CompressorJs(List<string> files)
        {
            while (true)
            {
                Console.WriteLine();
                Console.WriteLine("是否覆盖源文件？");
                Console.WriteLine("yes:用编译后的新文件覆盖源文件");
                Console.WriteLine("no:生成源文件名.min.js的文件，若.min.js文件已存在，则覆盖");
                Console.WriteLine("exit:退出");
                Console.Write("请输入:");
                string input = Console.ReadLine().ToLower().Trim();
                if (input == "yes")
                {
                    Console.WriteLine();
                    Console.WriteLine("开始编译JS文件");
                    Console.WriteLine();
                    CompressorJs(files, true);
                    Console.WriteLine();
                    Console.WriteLine("结束编译JS文件");
                    Console.WriteLine();
                    break;
                }
                else if (input == "no")
                {
                    Console.WriteLine();
                    Console.WriteLine("开始编译JS文件");
                    Console.WriteLine();
                    CompressorJs(files, false);
                    Console.WriteLine();
                    Console.WriteLine("结束编译JS文件");
                    Console.WriteLine();
                    break;
                }
                else if (input == "exit")
                {
                    break;
                }
            }
        }

        public static void CompressorJs(List<string> files, bool overwrite)
        {
            string js = null;
            foreach (string file in files)
            {
                if (!file.ToLower().EndsWith(".min.js"))
                {
                    js = Yahoo.Yui.Compressor.JavaScriptCompressor.Compress(File.ReadAllText(file));
                    if (overwrite)
                    {
                        File.WriteAllText(file, js);
                    }
                    else
                    {
                        File.WriteAllText(Path.GetDirectoryName(file) + "\\" + Path.GetFileNameWithoutExtension(file) + ".min.js", js);
                    }
                }
            }
        }

        public static void CompressorCss(List<string> files)
        {
            while (true)
            {
                Console.WriteLine();
                Console.WriteLine("是否覆盖源文件？");
                Console.WriteLine("yes:用编译后的新文件覆盖源文件");
                Console.WriteLine("no:生成源文件名.min.css的文件，若.min.css文件已存在，则覆盖");
                Console.WriteLine("exit:退出");
                Console.Write("请输入:");
                string input = Console.ReadLine().ToLower().Trim();
                if (input == "yes")
                {
                    Console.WriteLine();
                    Console.WriteLine("开始编译CSS文件");
                    Console.WriteLine();
                    CompressorCss(files, true);
                    Console.WriteLine();
                    Console.WriteLine("结束编译CSS文件");
                    Console.WriteLine();
                    break;
                }
                else if (input == "no")
                {
                    Console.WriteLine();
                    Console.WriteLine("开始编译CSS文件");
                    Console.WriteLine();
                    CompressorCss(files, false);
                    Console.WriteLine();
                    Console.WriteLine("结束编译CSS文件");
                    Console.WriteLine();
                    break;
                }
                else if (input == "exit")
                {
                    break;
                }
            }
        }

        public static void CompressorCss(List<string> files, bool overwrite)
        {
            string css = null;
            foreach (string file in files)
            {
                if (!file.ToLower().EndsWith(".min.css"))
                {
                    css = Yahoo.Yui.Compressor.CssCompressor.Compress(File.ReadAllText(file));
                    if (overwrite)
                    {
                        File.WriteAllText(file, css);
                    }
                    else
                    {
                        File.WriteAllText(Path.GetDirectoryName(file) + "\\" + Path.GetFileNameWithoutExtension(file) + ".min.css", css);
                    }
                }
            }
        }
    }
}
